﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Orchard.Environment.Extensions;

namespace MediaGarden.Images.Thumbnails
{
    /// <summary>
    /// 
    /// </summary>
    [OrchardFeature("MediaGarden.Images.Thumbnails")]
    public class Migrations : DataMigrationImpl
    {

        public int Create()
        {
            ContentDefinitionManager.AlterTypeDefinition("Thumbnail", cfg => cfg
                .WithPart("CommonPart")
                //         .WithPart("ImageViewportPart")
                .WithPart("ConnectorPart", part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Image,Video,Audio,Document,Embed,Binary,Page,BlogPost")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Image")
                    .WithSetting("ConnectorTypePartSettings.AllowMany", "false")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "ThumbnailFor")
                    ));

            ContentDefinitionManager.AlterTypeDefinition("ThumbnailFor", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("ConnectorPart", part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Image,Video,Audio,Document,Embed,Binary,Page,BlogPost")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Image")
                    .WithSetting("ConnectorTypePartSettings.AllowMany", "true")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "Thumbnail")
                    .WithSetting("ConnectorTypePartSettings.SocketDisplayName", "Used as thumbnail for")
                    ));
            return 1;
        }

        public int UpdateFrom1()
        {
            // Support versioning and timed publishing
            ContentDefinitionManager.AlterTypeDefinition("Thumbnail",
                cfg => cfg
                    .WithPart("PublishLaterPart")
                    .Draftable()
                    );
            ContentDefinitionManager.AlterTypeDefinition("ThumbnailFor",
                cfg => cfg
                    .WithPart("PublishLaterPart")
                    .Draftable()
                    );
            return 2;
        }
        public int UpdateFrom2()
        {
            // Disable feeds from thumbnails
            ContentDefinitionManager.AlterTypeDefinition("Thumbnail", cfg => cfg
                .WithPart("ConnectorPart", part => part
                    .WithSetting("AggregationTypePartSettings.ExposeFeed", "false")
                )
                );
            ContentDefinitionManager.AlterTypeDefinition("ThumbnailFor", cfg => cfg
                .WithPart("ConnectorPart", part => part
                    .WithSetting("AggregationTypePartSettings.ExposeFeed", "false")
                )
                );
            return 3;
        }

    }
}